<template>
  <div class="app-container">
    <el-card class="table_style">
      <el-form ref="ruleForm" :model="advert" :rules="rules" label-width="100px">
        <el-form-item label="广告名称" prop="name">
          <el-input v-model="advert.name" />
        </el-form-item>

        <el-form-item label="广告图片" prop="image">
          <el-upload
            class="avatar-uploader"
            :data="qiniuForm"
            :before-upload="beforeUpload"
            :on-success="uploadSuccess"
            action="https://upload.qiniup.com"
          >
            <img v-if="advert.image" :src="advert.image" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>

        <el-form-item label="广告排序" prop="sort">
          <el-input-number v-model="advert.sort" :min="1" :max="99" @change="handleChange" />
        </el-form-item>

        <el-form-item>
          <el-button @click="resetForm('ruleForm')">重置表单</el-button>
          <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { createAdvert } from '@/api/advert'
import advert from '@/mixins/advert'
export default {
  mixins: [advert],
  data() {
    return {
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (!valid) return
        const res = await createAdvert(this.advert)
        if (res.code === 20000) {
          this.$message({
            message: res.message,
            type: 'success'
          })
          this.$router.push({ name: 'Advert' })
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    handleChange(value) {
    }
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
